package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.ExampaperMapper;
import com.hqyj.dao.StudentMapper;
import com.hqyj.dao.SubjectMapper;
import com.hqyj.pojo.Exampaper;
import com.hqyj.pojo.ScoreManagement;
import com.hqyj.dao.ScoreManagementMapper;
import com.hqyj.pojo.Student;
import com.hqyj.pojo.Subject;
import com.hqyj.service.IScoreManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-11-12
 */
@Service
public class ScoreManagementServiceImpl extends ServiceImpl<ScoreManagementMapper, ScoreManagement> implements IScoreManagementService {

    //注入ScoreManagementMapper
    @Autowired
    private ScoreManagementMapper scoreManagementMapper;

    //注入SubjectMapper
    @Autowired
    private SubjectMapper subjectMapper;

    //注入StudentMapper
    @Autowired
    private StudentMapper studentMapper;

    //注入ExampaperMapper
    @Autowired
    private ExampaperMapper exampaperMapper;

    @Override
    public HashMap<String, Object> select(ScoreManagement scoreManagement) {
        System.err.println("=====================" + scoreManagement);
        //定义HashMap
        HashMap<String, Object> map = new HashMap<>();
        //定义分页对象
        Page<ScoreManagement> scoreManagementPage = new Page<>(scoreManagement.getPage(), scoreManagement.getRow());
        //定义条件构造器
        QueryWrapper<ScoreManagement> queryWrapper = new QueryWrapper<>();
        //设置排序，按分数倒序排序
        queryWrapper.orderByDesc("score");
        //设置查询条件
        //查询学生姓名===>将学生姓名转换为学生Id，进行条件查询
        Student student1 = studentMapper.selectByName(scoreManagement.getStudentName());
        //当执行条件查询是执行以下代码，否则不执行
        if (student1 != null) {
            scoreManagement.setStudentId(student1.getStudentId());
            queryWrapper.eq("student_id", scoreManagement.getStudentId());
        }
        //查询试卷名===>将试卷名转换为试卷Id
        Exampaper exampaper1 = exampaperMapper.selectByName(scoreManagement.getExamTitle());
        //当执行条件查询是执行以下代码，否则不执行
        if (exampaper1 != null) {
            scoreManagement.setExamId(exampaper1.getExamId());
            queryWrapper.eq("exam_id", scoreManagement.getExamId());
        }
        //查询分数
        queryWrapper.eq(scoreManagement.getScore() != null, "score", scoreManagement.getScore());
        //判断是否为管理员或是学生
        if (scoreManagement.getStudentId() != null){
            queryWrapper.eq("student_id",scoreManagement.getStudentId());
        }
        //获取分页结果集
        Page<ScoreManagement> page = scoreManagementMapper.selectPage(scoreManagementPage, queryWrapper);
        //获取学生姓名、科目名，并添加到list中
        List<ScoreManagement> scoreManagementList = page.getRecords();
        for (ScoreManagement management : scoreManagementList) {
            //设置学生对象
            management.setStudentName(studentMapper.selectById(management.getStudentId()).getStudentName());
            //设置科目对象
            if (management.getSubjectId() != null){
            Subject subject = subjectMapper.selectById(management.getSubjectId());
            management.setSubjectName(subject.getSubjectName());
            }
            //设置试卷名
            Exampaper exampaper = exampaperMapper.selectById(management.getExamId());
            management.setExamTitle(exampaper.getExamTitle());
        }
        //根据试卷ID查询试卷名
        List<String> optionList = scoreManagementMapper.selectExamName();
        System.err.println("==========optionList=============="+optionList);
        map.put("info", "success");
        map.put("list", page.getRecords());
        //添加下拉选项集合，用于查询平均数
        map.put("optionList", optionList);
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public HashMap<String, Object> getAvg(ScoreManagement scoreManagement) {
        HashMap<String, Object> map = new HashMap<>();
        Exampaper exampaper1 = exampaperMapper.selectByName(scoreManagement.getExamTitle());
        int avgScore = scoreManagementMapper.selectAvgScore(exampaper1.getExamId());
        System.err.println("========SCORE========="+avgScore);
        map.put("avgScore",avgScore);
        return map;
    }

    //求及格率
    @Override
    public HashMap<String ,Object> selectPassRate(){
        //HashMap嵌套List
        //List嵌套HashMap
        HashMap<String,Object> map = new HashMap<>();
        List<HashMap<String,Object>> passRateList = new ArrayList<>();
        List<ScoreManagement> scoreManagementList = scoreManagementMapper.selectPassRate();
        for (ScoreManagement scoreManagement : scoreManagementList) {
            HashMap<String,Object> map1 = new HashMap<>();
            Exampaper exampaper = exampaperMapper.selectById(scoreManagement.getExamId());
            map1.put("value",scoreManagement.getPassRate());
            map1.put("name",exampaper.getExamTitle());
            passRateList.add(map1);
        }
        map.put("passRateList",passRateList);
        return map;
    }
}
